/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of libxls -- A multiplatform, C/C++ library
 * for parsing Excel(TM) files.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2004 Komarov Valery
 * Copyright 2006 Christophe Leitienne
 * Copyright 2013 Bob Colbert
 * Copyright 2008-2013 David Hoerl
 *
 */

#pragma once

#include "ole.h"

#define XLS_RECORD_EOF          0x000A
#define XLS_RECORD_DEFINEDNAME  0x0018
#define XLS_RECORD_NOTE         0x001C
#define XLS_RECORD_1904         0x0022
#define XLS_RECORD_CONTINUE     0x003C
#define XLS_RECORD_WINDOW1      0x003D
#define XLS_RECORD_CODEPAGE     0x0042
#define XLS_RECORD_OBJ          0x005D
#define XLS_RECORD_MERGEDCELLS  0x00E5
#define XLS_RECORD_DEFCOLWIDTH  0x0055
#define XLS_RECORD_COLINFO      0x007D
#define XLS_RECORD_BOUNDSHEET   0x0085
#define XLS_RECORD_PALETTE      0x0092
#define XLS_RECORD_MULRK        0x00BD
#define XLS_RECORD_MULBLANK     0x00BE
#define XLS_RECORD_DBCELL       0x00D7
#define XLS_RECORD_XF           0x00E0
#define XLS_RECORD_MSODRAWINGGROUP   0x00EB
#define XLS_RECORD_MSODRAWING   0x00EC
#define XLS_RECORD_SST          0x00FC
#define XLS_RECORD_LABELSST     0x00FD
#define XLS_RECORD_EXTSST       0x00FF
#define XLS_RECORD_TXO          0x01B6
#define XLS_RECORD_HYPERREF     0x01B8
#define XLS_RECORD_BLANK        0x0201
#define XLS_RECORD_NUMBER       0x0203
#define XLS_RECORD_LABEL        0x0204
#define XLS_RECORD_BOOLERR      0x0205
#define XLS_RECORD_STRING       0x0207 // only follows a formula
#define XLS_RECORD_ROW          0x0208
#define XLS_RECORD_INDEX        0x020B
#define XLS_RECORD_ARRAY        0x0221 // Array-entered formula
#define XLS_RECORD_DEFAULTROWHEIGHT    0x0225
#define XLS_RECORD_FONT         0x0031 // spec says 0x0231 but Excel expects 0x0031
#define XLS_RECORD_FONT_ALT     0x0231
#define XLS_RECORD_WINDOW2      0x023E
#define XLS_RECORD_RK           0x027E
#define XLS_RECORD_STYLE        0x0293
#define XLS_RECORD_FORMULA      0x0006
#define XLS_RECORD_FORMULA_ALT  0x0406 // Apple Numbers bug
#define XLS_RECORD_FORMAT       0x041E
#define XLS_RECORD_BOF          0x0809

#define BLANK_CELL  XLS_RECORD_BLANK  // compat

#ifdef AIX
#pragma pack(1)
#else
#pragma pack(push, 1)
#endif

#include "xlstypes.h"


typedef struct BOF
{
    uint16_t id;
    uint16_t size;
}
BOF;

typedef struct BIFF
{
    uint16_t ver;
    uint16_t type;
    uint16_t id_make;
    uint16_t year;
    uint32_t flags;
    uint32_t min_ver;
    unsigned char buf[100];
}
BIFF;

typedef struct WIND1
{
    uint16_t xWn;
    uint16_t yWn;
    uint16_t dxWn;
    uint16_t dyWn;
    uint16_t grbit;
    uint16_t itabCur;
    uint16_t itabFirst;
    uint16_t ctabSel;
    uint16_t wTabRatio;
}
WIND1;

typedef struct BOUNDSHEET
{
    uint32_t	filepos;
    unsigned char	type;
    unsigned char	visible;
    unsigned char	name[];
}
BOUNDSHEET;

typedef struct ROW
{
    uint16_t	index;
    uint16_t	fcell;
    uint16_t	lcell;
    uint16_t	height;
    uint16_t	notused;
    uint16_t	notused2; //used only for BIFF3-4
    uint16_t	flags;
    uint16_t	xf;
}
ROW;

typedef struct COL
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
}
COL;


typedef struct FORMULA // BIFF8
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
	// next 8 bytes either a IEEE double, or encoded on a byte basis
    unsigned char	resid;
    unsigned char	resdata[5];
    uint16_t	res;
    uint16_t	flags;
    unsigned char	chn[4]; // BIFF8
    uint16_t	len;
    unsigned char	value[1]; //var
}
FORMULA;

typedef struct FARRAY // BIFF8
{
    uint16_t	row1;
    uint16_t	row2;
    unsigned char	col1;
    unsigned char	col2;
    uint16_t	flags;
    unsigned char	chn[4]; // BIFF8
    uint16_t	len;
    unsigned char	value[1]; //var
}
FARRAY;

typedef struct RK
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
    DWORD_UA value;
}
RK;

typedef struct MULRK
{
    uint16_t	row;
    uint16_t	col;
	struct {
		uint16_t	xf;
		DWORD_UA value;
	}		rk[];
	//WORD	last_col;
}
MULRK;

typedef struct MULBLANK
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf[];
	//WORD	last_col;
}
MULBLANK;

typedef struct BLANK
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
}
BLANK;

typedef struct LABEL
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
    unsigned char	value[1]; // var
}
LABEL;
typedef LABEL LABELSST;

typedef struct BOOLERR
{
    uint16_t    row;
    uint16_t    col;
    uint16_t    xf;
    unsigned char    value;
    unsigned char    iserror;
}
BOOLERR;

typedef struct SST
{
    uint32_t	num;
    uint32_t	numofstr;
    unsigned char	strings;
}
SST;

typedef struct XF5
{
    uint16_t	font;
    uint16_t	format;
    uint16_t	type;
    uint16_t	align;
    uint16_t	color;
    uint16_t	fill;
    uint16_t	border;
    uint16_t	linestyle;
}
XF5;

typedef struct XF8
{
    uint16_t	font;
    uint16_t	format;
    uint16_t	type;
    unsigned char	align;
    unsigned char	rotation;
    unsigned char	ident;
    unsigned char	usedattr;
    uint32_t	linestyle;
    uint32_t	linecolor;
    uint16_t	groundcolor;
}
XF8;

typedef struct BR_NUMBER
{
    uint16_t	row;
    uint16_t	col;
    uint16_t	xf;
    double value;
}
BR_NUMBER;

typedef struct COLINFO
{
    uint16_t	first;
    uint16_t	last;
    uint16_t	width;
    uint16_t	xf;
    uint16_t	flags;
    uint16_t	notused;
}
COLINFO;

typedef struct MERGEDCELLS
{
    uint16_t	rowf;
    uint16_t	rowl;
    uint16_t	colf;
    uint16_t	coll;
}
MERGEDCELLS;

typedef struct FONT
{
    uint16_t	height;
    uint16_t	flag;
    uint16_t	color;
    uint16_t	bold;
    uint16_t	escapement;
    unsigned char	underline;
    unsigned char	family;
    unsigned char	charset;
    unsigned char	notused;
    unsigned char	name;
}
FONT;

typedef struct FORMAT
{
    uint16_t	index;
    unsigned char	value[0];
}
FORMAT;

#pragma pack(pop)

//---------------------------------------------------------

struct st_sheet_data
{
	uint32_t filepos;
	unsigned char visibility;
	unsigned char type;
	unsigned char* name;
};

typedef	struct st_sheet
{
    uint32_t count;        // Count of sheets
    struct st_sheet_data* sheet;
}
st_sheet;

struct st_font_data
{
	uint16_t	height;
	uint16_t	flag;
	uint16_t	color;
	uint16_t	bold;
	uint16_t	escapement;
	unsigned char	underline;
	unsigned char	family;
	unsigned char	charset;
	unsigned char*	name;
};

typedef	struct st_font
{
    uint32_t count;		// Count of FONT's  
    struct st_font_data* font;
}
st_font;

struct st_format_data
{
	uint16_t index;
	unsigned char *value;
};
typedef struct st_format
{
    uint32_t count;		// Count of FORMAT's 
    struct st_format_data* format;
}
st_format;

struct st_xf_data
{
	uint16_t	font;
	uint16_t	format;
	uint16_t	type;
	unsigned char	align;
	unsigned char	rotation;
	unsigned char	ident;
	unsigned char	usedattr;
	uint32_t	linestyle;
	uint32_t	linecolor;
	uint16_t	groundcolor;
};

typedef	struct st_xf
{
    uint32_t count;	// Count of XF
    //	XF** xf;
    struct st_xf_data* xf;
}
st_xf;

struct str_sst_string
{
	unsigned char* str;
};

typedef	struct st_sst
{
    uint32_t count;
    uint32_t lastid;
    uint32_t continued;
    uint32_t lastln;
    uint32_t lastrt;
    uint32_t lastsz;
    struct str_sst_string* string;
}
st_sst;

struct st_cell_data
{
	uint16_t	id;
	uint16_t	row;
	uint16_t	col;
	uint16_t	xf;
	unsigned char*	str;		// String value;
	double	d;
	int32_t	l;
	uint16_t	width;		// Width of col
	uint16_t	colspan;
	uint16_t	rowspan;
	unsigned char	isHidden;	// Is cell hidden
};

typedef	struct st_cell
{
    uint32_t count;
    struct st_cell_data* cell;
}
st_cell;

struct st_row_data
{
	uint16_t index;
	uint16_t fcell;
	uint16_t lcell;
	uint16_t height;
	uint16_t flags;
	uint16_t xf;
	unsigned char xfflags;
	st_cell cells;
};

typedef	struct st_row
{
    //	DWORD count;
    uint16_t lastcol;	// numCols - 1
    uint16_t lastrow;	// numRows - 1
    struct st_row_data* row;
}
st_row;

struct st_colinfo_data
{
	uint16_t	first;
	uint16_t	last;
	uint16_t	width;
	uint16_t	xf;
	uint16_t	flags;
};

typedef	struct st_colinfo
{
    uint32_t count;				// Count of COLINFO
    struct st_colinfo_data* col;
}
st_colinfo;

typedef struct xlsWorkBook
{
    //FILE*		file;
    OLE2Stream*	olestr;
    int32_t		filepos;		// position in file

    //From Header (BIFF)
    unsigned char		is5ver;
    unsigned char		is1904;
    uint16_t		type;
    uint16_t		activeSheetIdx;	// index of the active sheet

    //Other data
    uint16_t		codepage;		// Charset codepage
    char*		charset;
    st_sheet	sheets;
    st_sst		sst;			// SST table
    st_xf		xfs;			// XF table
    st_font		fonts;
    st_format	formats;		// FORMAT table

	char		*summary;		// ole file
	char		*docSummary;	// ole file
}
xlsWorkBook;

typedef struct xlsWorkSheet
{
    uint32_t		filepos;
    uint16_t		defcolwidth;
    st_row		rows;
    xlsWorkBook *workbook;
    st_colinfo	colinfo;
}
xlsWorkSheet;


typedef struct st_cell_data xlsCell;
typedef	struct st_row_data xlsRow;


typedef struct xls_summaryInfo
{
	unsigned char		*title;
	unsigned char		*subject;
	unsigned char		*author;
	unsigned char		*keywords;
	unsigned char		*comment;
	unsigned char		*lastAuthor;
	unsigned char		*appName;
	unsigned char		*category;
	unsigned char		*manager;
	unsigned char		*company;
}
xlsSummaryInfo;

typedef void (*xls_formula_handler)(uint16_t bof, uint16_t len, unsigned char *formula);


